package com.zkr.kfkq.service;

import com.zkr.kfkq.mapper.KFKQMapper;
import com.zkr.kfkq.model.*;
import com.zkr.kqsx.model.KQSXVo;
import com.zkr.subclass.enummodel.StatusEnum;
import com.zkr.util.AllRecords;
import com.zkr.util.ResultBuilderVo;
import com.zkr.util.ResultVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by lenovo on 2021/1/16.
 */
@Service
public class KFKQServiceImpl implements KFKQService {

    @Autowired
    private KFKQMapper kfkqMapper;

    @Override
    public ResultVo queryPage(KFKQSelectVo kfkqSelectVo) {
        int count = kfkqMapper.queryCount(kfkqSelectVo);
        List<KFKQVo> kfkqVoList = kfkqMapper.queryPage(kfkqSelectVo);
        for (KFKQVo kfkqVo: kfkqVoList){
            Integer status = kfkqVo.getStatus() == true?1:0;
            for (StatusEnum c:StatusEnum.values()){
                if (c.getValue().equals(status)){
                    kfkqVo.setStatusName(c.getName());
                }
            }
        }
        AllRecords allRecords = new AllRecords();
        allRecords.setPageIndex(kfkqSelectVo.getPageIndex());
        allRecords.setDataList(kfkqVoList);
        allRecords.setPageSize(kfkqSelectVo.getPageSize());
        allRecords.setTotalNumber(count);
        allRecords.resetTotalNumber(count);
        return ResultBuilderVo.success(allRecords);

    }

    @Override
    public ResultVo updateStatus(@Valid KFKQUpdateStatusVo kfkqUpdateStatusVo) {
        KQVo kqVo = new KQVo();
        BeanUtils.copyProperties(kfkqUpdateStatusVo,kqVo);
        kqVo.setUpdateTime(new Date());
        kqVo.setUpdateName("李四");
        kfkqMapper.updateStatus(kqVo);
        return ResultBuilderVo.success();

    }

    @Override
    public ResultVo queryCode() {
        List<KFVo> kfVoList = kfkqMapper.querykf();
        return ResultBuilderVo.success(kfVoList);


    }

    @Override
    public ResultVo query(Integer id) {
        KFKQVo kfkqVo = kfkqMapper.query(id);
        List<KQSXVo> list= new ArrayList<>();
        kfkqVo.setBusinessAttribute(list);
        return ResultBuilderVo.success(kfkqVo);

    }

    @Override
    public ResultVo queryEulogy() {
        List<EulogyVo> eulogyVoList = kfkqMapper.queryEulogy();
        return ResultBuilderVo.success(eulogyVoList);

    }

   @Override
   @Transactional
    public ResultVo add(@Valid KFKQAddVo kfkqAddVo) {
        KFKQVo kqCode = kfkqMapper.queryCode(kfkqAddVo.getCode(),kfkqAddVo.getWareHouseCode());
        if (kqCode != null){
            return ResultBuilderVo.error("库区编码已经存在");
        }
        KFKQVo kqName = kfkqMapper.queryName(kfkqAddVo.getName(),kfkqAddVo.getWareHouseName());
        if (kqName != null){
            return ResultBuilderVo.error("库区名称已经存在");
        }
        KFKQVo kfkqVo = new KFKQVo();
       //把左边赋值到右边
         BeanUtils.copyProperties(kfkqAddVo,kfkqVo);
        kfkqVo.setCreateName("李四");
        kfkqVo.setCreateTime(new Date());
        kfkqVo.setUpdateName("李四");
        kfkqVo.setUpdateTime(new Date());

       //入库
       kfkqMapper.add(kfkqVo);

       //设置库区业务属性
       List<ReservoirAttributesVo> list = kfkqAddVo.getBusinessAttribute();
       //添加新的业务属性
       if (list!=null && list.size()>0){
           kfkqMapper.addAttribute(kfkqVo.getId(),list);
       }
      /* Integer id = kfkqMapper.queryCodes(kfkqAddVo.getCode());
       kfkqMapper.add2(kfkqAddVo.getBusinessAttribute(),id);*/
       return ResultBuilderVo.success();
    }

    @Override
    @Transactional
    public ResultVo update(@Valid KFKQUpdateVo kfkqUpdateVo) {
        KFKQVo kqName = kfkqMapper.queryName(kfkqUpdateVo.getName(),kfkqUpdateVo.getWareHouseName());
        if (kqName != null) {
            if (!(kqName.getId().equals(kqName.getId()))) {
                return ResultBuilderVo.error("库区名称不能重复!");
            }
        }
        KFKQVo kqCode = kfkqMapper.queryCode(kfkqUpdateVo.getCode(),kfkqUpdateVo.getWareHouseName());
        if (kqCode != null){
            if (!(kqCode.getId().equals(kqCode.getId()))){
                return ResultBuilderVo.error("库区编码不能重复!");
            }
        }
        KFKQVo kfkqVo = new KFKQVo();
        BeanUtils.copyProperties(kfkqUpdateVo,kfkqVo);
        kfkqVo.setUpdateName("李四");
        kfkqVo.setUpdateTime(new Date());
        kfkqMapper.update(kfkqVo);


        kfkqMapper.deleteAttribute(kfkqUpdateVo.getId());
        //设置库区业务属性
        List<ReservoirAttributesVo> list = kfkqUpdateVo.getBusinessAttribute();
        //添加新的业务属性
        if (list!=null && list.size()>0){
            kfkqMapper.addAttribute(kfkqVo.getId(),list);
        }
        return ResultBuilderVo.success();

    }

}
